/**
 *

import javax.naming.spi.DirStateFactory.Result;

/**
 ****************************
 * TODO
 * @author Chen Fan
 * @version 1.0
 * time 2021年12月26日
 ****************************
 */

public class CircleIntQueue {
    public static final int TOTAL_SPACE = 10;
    int[] data;
    int front;
    int rear;

    //构造函数，构造循环队列
    public CircleIntQueue() {
        data = new int[TOTAL_SPACE];
        front = 0;
        rear = 0;
    }// Of construct

    //入队操作
    public void enqueue(int paraValue) {
        if ((rear + 1) % TOTAL_SPACE == front) {
            System.out.println("Queue full.");
            return;
        } // Of if
        data[rear % TOTAL_SPACE] = paraValue;
        rear = (rear + 1) % TOTAL_SPACE;
    }// Of enqueue

    // 出队
    public int dequeue() {
        if(rear==front) {
            System.out.println("No element in the queue");
            return -1;
        }// Of if

        int resultValue = data[front%TOTAL_SPACE];
        front=(front+1)%TOTAL_SPACE;
        return resultValue;
    }// Of dequeue

    //重写函数
    public String toString() {
        String resulString = "";
        if(rear==front) {
            return "empty";
        }
        //设置遍历标记
        int rear1=rear,front1=front;
        while(rear1!=front1) {
            resulString+=data[front1%TOTAL_SPACE]+",";
            front1=(front1+1)%TOTAL_SPACE;
        }
        return resulString;
    }

    //测试函数
    public static void main(String args[]) {
        CircleIntQueue tempQueue = new CircleIntQueue();
        System.out.println("Initialized, the list is: " + tempQueue.toString());

        for (int i = 0; i < 11; i++) {
            tempQueue.enqueue(i + 1);
        } // Of for i
        System.out.println("Enqueue, the queue is: " + tempQueue.toString());

        int tempValue = tempQueue.dequeue();
        System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
//
        for (int i = 0; i < 6; i++) {
            tempQueue.enqueue(i + 10);
            System.out.println("Enqueue, the queue is: " + tempQueue.toString());
        } // Of for i
//
        for (int i = 0; i < 3; i++) {
            tempValue = tempQueue.dequeue();
            System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
        } // Of for i
//
//        for (int i = 0; i < 6; i++) {
//            tempQueue.enqueue(i + 100);
//            System.out.println("Enqueue, the queue is: " + tempQueue.toString());
//        } // Of for i
    }// Of main
}
